﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Trogsoft.IRC;

namespace TestApplication {
class Program {

    public static IRCClient irc;

    static void Main(string[] args) {

        irc = new IRCClient("irc.afternet.org",6667);
        irc.IrcNick = "Cowbell";
        irc.AddChannelToJoin("#tstest");
        irc.Connect();

        irc.OnStatusMessage += new IRCClient.StatusMessageHandler(irc_OnStatusMessage);
        irc.OnDisconnect += new IRCClient.DisconnectHandler(irc_OnDisconnect);
        irc.OnConnect += new IRCClient.ConnectHandler(irc_OnConnect);
        irc.OnJoinChannel += new IRCClient.JoinChannelHandler(irc_OnJoinChannel);
        irc.OnModeChange += new IRCClient.ModeChangeHandler(irc_OnModeChange);
        irc.OnNickChange += new IRCClient.NickChangeHandler(irc_OnNickChange);
        irc.OnPartChannel += new IRCClient.PartChannelHandler(irc_OnPartChannel);
        irc.OnPing += new IRCClient.PingHandler(irc_OnPing);
        irc.OnPrivateMessage += new IRCClient.PrivateMessageHandler(irc_OnPrivateMessage);
        irc.OnTopicChange += new IRCClient.TopicChangeHandler(irc_OnTopicChange);

        Console.ReadLine();

        irc.Disconnect();

        irc.Connect();

        Console.ReadLine();

    }

    static void irc_OnStatusMessage(object sender, IrcEvent e)
    {
        Console.WriteLine(e.EventData);
    }

    static void irc_OnTopicChange(object sender, IrcEvent e) {
        Console.WriteLine("Topic changed to: " + e.EventData);
    }

    static void irc_OnPrivateMessage(object sender, IrcEvent e) {
        Console.Write("<" + e.Nick + "> " + e.EventData);
    }

    static void irc_OnPing(object sender) {
        Console.WriteLine("PING!");
    }

    static void irc_OnPartChannel(object sender, IrcEvent e) {
        Console.WriteLine("Part channel: " + e.Nick);
    }

    static void irc_OnNickChange(object sender, IrcEvent e) {
        Console.WriteLine("Nick change from " + e.Nick + " to " + e.TargetNicks);
    }

    static void irc_OnModeChange(object sender, IrcEvent e) {
        Console.WriteLine("Mode change: " + e.EventData+ " on "+e.TargetNicks);
    }

    static void irc_OnJoinChannel(object sender, IrcEvent e) {
        Console.WriteLine("Joined: " + e.Nick);
    }

    static void irc_OnError(object sender) {
        Console.WriteLine("Error");
    }

    static void irc_OnConnect(object sender) {
        Console.WriteLine("Connected...");
    }

    static void irc_OnDisconnect(object sender) {

        Console.WriteLine("Disconnected");

        int i = 0;
        while (irc.IsConnected == false) {
                
            try {
                Console.WriteLine("Attempting to connect...");
                irc.Connect();
            } catch { }

            System.Threading.Thread.Sleep(5000);

            i++;
            if (i > 1000) {
                break;
            }

        }

    }

}
}
